package de.ret_world.openrpgstudio.pluginmanager;

import buoy.widget.*;
import de.ret_world.openrpgstudio.editor.Project;
import de.ret_world.openrpgstudio.editor.helpers.Internationalization;
import de.ret_world.openrpgstudio.editor.plugininterfaces.OptionMenuPlugin;
import java.awt.Insets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.BorderFactory;
import org.java.plugin.registry.PluginDescriptor;
import org.java.plugin.registry.PluginPrerequisite;

/**
 * The PluginTool class is the main class of the PluginManager plugin.
 *
 * @author Emu
 */
public class PluginTool extends OptionMenuPlugin {

    /**
     * This plug-in ID.
     */
    public static final String PLUGIN_ID = "de.ret_world.openrpgstudio.pluginmanager";
    //Variables
    private ArrayList<BCheckBox> boxes;
    private String[] trusted = new String[]{"de.ret_world.openrpgstudio.editor", "de.ret_world.openrpgstudio.engine", "de.ret_world.openrpgstudio.pluginmanager"};

    public PluginTool() {
        super();
    }

    @Override
    public void init(BScrollPane container) {
        boxes = new ArrayList<>();
        List<String> trustedPlugins = Arrays.asList(trusted);
        Internationalization intn = Internationalization.getInstance();

        LayoutInfo li = new LayoutInfo(LayoutInfo.WEST, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null);

        ColumnContainer cc = new ColumnContainer();

        cc.add(new BLabel(intn.getString("PM.PLUGINS1")));
        cc.add(new BLabel(intn.getString("PM.PLUGINS2")));
        cc.add(new BLabel(intn.getString("PM.PLUGINS3")));
        cc.add(new BLabel(intn.getString("PM.PLUGINS4")));

        for (PluginDescriptor descr : manager.getRegistry().getPluginDescriptors()) {
            log.debug(descr);

            ColumnContainer ccp = new ColumnContainer();
            ccp.add(new BLabel(intn.getString("PM.PVERSION") + ":\t " + descr.getVersion().toString()), li);

            ccp.add(new BLabel(intn.getString("PM.DEPENDSON") + ":"), li);
            String depends = "[";
            for (PluginPrerequisite pr : descr.getPrerequisites()) {
                if (pr.isOptional()) {
                    depends += "(";
                }
                depends += pr.getPluginId();
                if (pr.isOptional()) {
                    depends += "), ";
                } else {
                    depends += ", ";
                }
            }
            if (depends.length() > 1) {
                depends = depends.substring(0, depends.length() - 2) + "]";
            } else {
                depends = intn.getString("NONE");
            }

            ccp.add(new BLabel(depends), li);

            BCheckBox cbx = new BCheckBox(intn.getString("PM.ENABLED"), manager.isPluginEnabled(descr));
            cbx.setName(descr.getId());
            if (trustedPlugins.contains(descr.getId())) {
                cbx.setEnabled(false);
            } else {
                boxes.add(cbx);
            }

            ccp.add(cbx, li);

            cc.add(new BOutline(ccp, BorderFactory.createTitledBorder(descr.getId())), li);
        }

        container.setContent(cc);
    }

    @Override
    public void save() {
        for (BCheckBox cbx : boxes) {
            String id = cbx.getName();
            Project project = Project.getInstance();
            project.getConfig().setProperty("plugin.enabled:" + id, Boolean.toString(cbx.getState()));

            if (!cbx.getState()) {
                log.info("Disable Plugin " + id);
                manager.disablePlugin(manager.getRegistry().getPluginDescriptor(id));
            } else {
                if (!manager.isPluginEnabled(manager.getRegistry().getPluginDescriptor(id))) {
                    log.info("Enable Plugin " + id);
                    manager.enablePlugin(manager.getRegistry().getPluginDescriptor(id), false);
                }
            }

            try {
                project.save();
            } catch (IOException ex) {
                log.error(ex);
            }
        }
    }
}
